package com.dingding.service.user.core.dao.impl;

import java.math.BigDecimal;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Useraccount;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.user.core.dao.IUserAccountDao;
import com.dingding.service.user.core.sql.UserAccountSQL;

/**
 * 
 * @ClassName: UserAccountServiceImpl
 * @Description:用户账户Service实现类
 * @author zhaochenguang
 * @date 2015-10-20 上午09:52:25
 * @version 叮叮搭车V2.0
 */
@Component("userAccountDao")
public class UserAccountDaoImpl extends BaseDaoImpl<Useraccount> implements IUserAccountDao {

	/**
	 * 
	 * @Title: addUserAccount 
	 * @Description: 添加用户账户信息 
	 * @param userLoginId 登录id 
	 * @return 
	 * @author CHEHUANBO
	 * 
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void addUserAccount(String userLoginId) {

		Useraccount useraccount = new Useraccount();
		// 为用户账户表设置主键
		useraccount.setUaId(ToolUtil.getUid());
		// 用户登录ID
		useraccount.setLoginId(userLoginId);
		// 账户余额
		useraccount.setCurrencyAmount(new BigDecimal(0));
		// 账户状态
		useraccount.setStatus(1);
		// 累计收入
		useraccount.setTotalIncome(new BigDecimal("0"));
		// 累计提现
		useraccount.setTotalDeposit(new BigDecimal("0"));
		// 累计支出
		useraccount.setTotalExpend(new BigDecimal("0"));
		try {
			// 向用户账户表插入一条记录
			super.save(useraccount);
		} catch (Exception e) {
			log.error("添加用户账户信息出现异常！", e);
		}

	}

	/**
	 * 
	 * 
	 * @Title: refund 
	 * @Description:退款
	 * @param  money 退款金额
	 * @return boolean true 成功 false 失败
	 * @author CHEHUANBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean refund(Double money, String passengerId) {

		int row = 0;

		try {

			// 执行给乘客退款操作 返回值 1：退款成功 0:退款失败
			row = super.updateSqlRow(UserAccountSQL.PASSENGER_REFUND, money, passengerId);

			if (row == 0) { // 退款失败
				return false;
			}

		} catch (Exception e) {
			log.info("退款时，出现异常!  类：" + this.getClass() + " passengerRefund", e);
		}

		return true;

	}

}
